//
//  HomeViewController.m
//  Dome
//
//  Created by 郭松伟 on 16/4/15.
//  Copyright © 2016年 Dxy. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeMuneViewController.h"
#import "TimeTableViewController.h"
#import "RecommendViewController.h"
#import <Masonry/Masonry.h>


#import "BJShoppingOnlineCollectionViewController.h"

@interface HomeViewController ()<UISearchControllerDelegate, UISearchDisplayDelegate, UISearchBarDelegate>
@property (nonatomic ,strong) UIButton *wsgw;

@property (nonatomic, strong) UITableView *subTableView;
@property (nonatomic, strong) UISearchDisplayController *searchController;
@property (nonatomic, strong) UISearchBar *searchBar;


@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addView];

    
    [self addSearchBar]; // 添加搜索框
 
    self.view.backgroundColor =[UIColor colorWithRed:238/255.0 green:238/255.0 blue:238/255.0 alpha:1];
    //测试
    self.navigationController.navigationBar.backgroundColor =[UIColor colorWithRed:52/255.0 green:146/255.0 blue:233/255.0 alpha:1];

}

- (void)addSearchBar {
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.navigationItem.titleView.frame.size.width, 44)];
    
    _searchBar.delegate = self;
    
    self.navigationItem.titleView = _searchBar;

    // 搜索框の背景文字
    self.searchBar.placeholder = @"请输入要搜索の内容";
    
    _searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
    
    _searchController.delegate = self;
    
//    _searchController.searchResultsDataSource = self;
//    
//    _searchController.searchResultsDelegate = self;
    
    // 设置显示控制器の代理
    _searchController.delegate = self;
}

#pragma mark - searchBarDelegate

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:YES];
    UIButton *cancelButton = [searchBar valueForKey:@"_cancelButton"];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];

    [self.searchDisplayController setActive:YES];  // 显示或隐藏搜索界面
    return YES;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:NO];
    [searchBar resignFirstResponder];
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    searchBar.frame = CGRectMake(8, 0, self.navigationItem.titleView.frame.size.width, 44);
    [self.searchDisplayController setActive:NO];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(void)addView{

//     _topview = [[HomeTopView alloc]init];
//    [self.view addSubview:_topview];
//    
//    [_topview mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.view.mas_top).with.offset(60);
//        make.height.equalTo(@60);
//        make.left.equalTo(self.view.mas_left);
//        make.right.equalTo(self.view.mas_right);
//    }];
//    [_topview addView];
    
    
    HomeMuneViewController *homeController = [[HomeMuneViewController alloc]init];
    
    [self.view addSubview:homeController.view];
    
    [homeController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(60);
        make.height.equalTo(@138);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
    }];
    
    RecommendViewController *recommendVC = [[RecommendViewController alloc]init];
    [self.view addSubview:recommendVC.view];
    
    [recommendVC.view  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(homeController.view.mas_bottom).with.offset(17.5);
        make.height.equalTo(@104);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
    }];
    
    TimeTableViewController *timeTableVC = [[TimeTableViewController alloc]init];
    [self.view addSubview:timeTableVC.view];
    
    [timeTableVC.view  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(recommendVC.view.mas_bottom).with.offset(17.5);
        make.bottom.equalTo(self.view.mas_bottom);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
    }];
    
    
    
    
}
- (void)click {
    
//    ShoppingTableViewController *ickImageViewController = [[ShoppingTableViewController alloc] init];
//    [self.navigationController pushViewController: ickImageViewController animated:true];
    
    UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc] init];
//    layout.minimumLineSpacing = 10;
//    //    layout.minimumInteritemSpacing = 2;
//    layout.itemSize = CGSizeMake(60, 80);
//    layout.sectionInset = UIEdgeInsetsMake(15, 15, 5, 15);
    
    BJShoppingOnlineCollectionViewController *ickImageViewController = [[BJShoppingOnlineCollectionViewController alloc] initWithCollectionViewLayout:layout];
    
    [self.navigationController pushViewController: ickImageViewController animated:true];
    
}



@end
